package com.huawei.esdk.tp.business.common.sitemgr;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.xml.datatype.XMLGregorianCalendar;

import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.platform.common.config.ConfigManager;
import com.huawei.esdk.platform.common.exception.SDKException;
import com.huawei.esdk.platform.common.utils.DateUtils;
import com.huawei.esdk.platform.common.utils.NumberUtils;
import com.huawei.esdk.platform.common.utils.StringUtils;
import com.huawei.esdk.tp.base.BaseService;
import com.huawei.esdk.tp.common.ErrInfo;
import com.huawei.esdk.tp.domain.model.Site;
import com.huawei.esdk.tp.domain.model.SiteInfoCommService;
import com.huawei.esdk.tp.domain.model.bean.DateTimeRange;
import com.huawei.esdk.tp.domain.model.bean.PagedList;
import com.huawei.esdk.tp.domain.model.bean.QuerySites;
import com.huawei.esdk.tp.domain.model.bean.SiteFreeBusyRes;

public class SiteManagerService extends BaseService
{
    //    private String span;
    
    private SiteInfoCommService siteInfoCommService = null;
    
    /**
     * 查询所有会场
     * 
     * @return
     * @throws SDKException 
     */
    public SDKResult<PagedList<Site>> queryAllSites()
        throws SDKException
    {
        // modify by c00316442 2015.2.3, findbugs DLS_DEAD_LOCAL_STORE
        QuerySites querySites = new QuerySites();
        querySites.setNumberPerPage(getTotalNumber());
        querySites.setCurrentPage(1);
        return Site.querySites(querySites);
    }
    
    private int getTotalNumber()
    {
        return NumberUtils.parseIntValue(ConfigManager.getInstance().getValue("common.queryAllSites.totalNumber"));
    }
    
    /**
     * 通过uri查询会场 * @param siteUri 会场uri /** * @return SDKResult
     * @throws SDKException 
     */
    public SDKResult<Site> querySiteByURI(String siteUri)
        throws SDKException
    {
        SDKResult<Site> site = new SDKResult<Site>();
        if (StringUtils.isEmpty(siteUri))
        {
            site.setErrCode(ErrInfo.SITE_URI_ISNULL_ERRORCODE);
            return site;
        }
        QuerySites querySites = new QuerySites();
        querySites.setNumberPerPage(getTotalNumber());
        querySites.setCurrentPage(1);
        
        SDKResult<PagedList<Site>> result = Site.querySites(querySites);
        
        site.setDescription(result.getDescription());
        site.setErrCode(result.getErrCode());
        site.setSubSystem(result.getSubSystem());
        if (result.getResult().getRecords().size() > 0)
        {
            List<Site> sites = result.getResult().getRecords();
            for (int i = 0; i < sites.size(); i++)
            {
                if (sites.get(i).getUri().equals(siteUri))
                {
                    site.setResult(sites.get(i));
                    break;
                }
            }
        }
        return site;
    }
    
    /**
     * 通过名称查找会场 * @param siteName /** * @return
     * @throws SDKException 
     */
    public SDKResult<List<Site>> querySiteByName(String siteName)
        throws SDKException
    {
        SDKResult<List<Site>> sites = new SDKResult<List<Site>>();
        if (StringUtils.isEmptyWihthoutTrim(siteName))
        {
            sites.setErrCode(ErrInfo.SITE_Name_ISNULL_ERRORCODE);
            List<Site> list = new LinkedList<Site>();
            sites.setResult(list);
            return sites;
        }
        QuerySites querySites = new QuerySites();
        querySites.setNumberPerPage(getTotalNumber());
        querySites.setCurrentPage(1);
        
        SDKResult<PagedList<Site>> result = Site.querySites(querySites);
        
        List<Site> list = new LinkedList<Site>();
        
        sites.setDescription(result.getDescription());
        sites.setErrCode(result.getErrCode());
        sites.setSubSystem(result.getSubSystem());
        
        if (result.getResult().getRecords().size() > 0)
        {
            List<Site> siteList = result.getResult().getRecords();
            for (int i = 0; i < siteList.size(); i++)
            {
                if (siteList.get(i).getName().contains(siteName))
                {
                    list.add(siteList.get(i));
                }
            }
        }
        sites.setResult(list);
        return sites;
    }
    
    /**
     * 查询单个会场忙闲状态 * @param siteUri * @return SDKResult<Integer>
     * @throws SDKException 
     */
    public SDKResult<List<Site>> querySiteFreeBusy(String siteUri)
        throws SDKException
    {
        SDKResult<List<Site>> result = new SDKResult<List<Site>>();
        Site site = new Site();
        
        // 设置会场URI
        List<String> siteUris = new ArrayList<String>();
        siteUris.add(siteUri);
        // 设置查询时间段
        DateTimeRange timeRange = new DateTimeRange();
        XMLGregorianCalendar start = DateUtils.getCurrentUTCTime();
        timeRange.setStart(DateUtils.toDate(start));
        String timeSpan = "PT" + getSpan() + "M";
        timeRange.setSpan(timeSpan);
        
        SiteFreeBusyRes response = site.querySiteFreeBusy(siteUris, timeRange);
        
        // 解析response
        result.setErrCode(response.getResult().getErrCode());
        if (0 == result.getErrCode())
        {
            result.setResult(response.getSiteFreeBusyStateList());
        }
        return result;
    }
    
    private String getSpan()
    {
        return ConfigManager.getInstance().getValue("common.querySiteFreeBusy.span");
    }
    
    /**
     * 查询多个会场忙闲状态 * @param siteUris * @param timeRange * @return
     * SDKResult<List<SitesStatusMap>>
     * @throws SDKException 
     */
    public SDKResult<List<Site>> querySitesFreeBusy(List<String> siteUris, Integer timeRange)
        throws SDKException
    {
        SDKResult<List<Site>> result = new SDKResult<List<Site>>();
        Site site = new Site();
        // 设置会场URI
        List<String> siteUrisList = new ArrayList<String>();
        siteUrisList.addAll(siteUris);
        // 设置查询时间段
        DateTimeRange tRange = new DateTimeRange();
        XMLGregorianCalendar start = DateUtils.getCurrentUTCTime();
        tRange.setStart(DateUtils.toDate(start));
        if (null == timeRange || 0 > timeRange)
        {
            result.setErrCode(ErrInfo.SITE_TIMERANGE_ERRORCODE);
            return result;
        }
        String span = "PT" + timeRange + "M";
        tRange.setSpan(span);
        
        SiteFreeBusyRes response = site.querySiteFreeBusy(siteUrisList, tRange);
        
        result.setErrCode(response.getResult().getErrCode());
        if (0 == result.getErrCode())
        {
            result.setResult(response.getSiteFreeBusyStateList());
        }
        
        return result;
    }
    
    public SDKResult<List<Site>> querySiteStatus(List<String> siteUris, DateTimeRange timeRange)
        throws SDKException
    {
        SDKResult<List<Site>> result = new SDKResult<List<Site>>();
        if (null == siteUris || 0 == siteUris.size())
        {
            result.setErrCode(ErrInfo.SITE_URI_ISNULL_ERRORCODE);
            return result;
        }
        
        if (null == timeRange)
        {
            result.setErrCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
            return result;
        }
        
        Site site = new Site();
        SiteFreeBusyRes response = site.querySiteFreeBusy(siteUris, timeRange);
        
        result.setErrCode(response.getResult().getErrCode());
        if (0 == result.getErrCode())
        {
            List<Site> siteList = response.getSiteFreeBusyStateList();
            result.setResult(siteList);
        }
        return result;
    }
    
    public SiteInfoCommService getSiteInfoCommService()
    {
        return siteInfoCommService;
    }
    
    public void setSiteInfoCommService(SiteInfoCommService siteInfoCommService)
    {
        this.siteInfoCommService = siteInfoCommService;
    }
    
}
